Builds and display single and multidimensional arrays.

The following code example uses GetLowerBound and GetUpperBound to initialize two arrays, one-dimensional and multidimensional.

C# .NET

public static String DoTest()
{
         string strRet = "";
         // Creates a new one-dimensional Array of type Int32.
         Array my1DIntArray = Array.CreateInstance( typeof(Int32), 5 );

         // Uses GetLowerBound and GetUpperBound in the for loop.
         for ( int i = my1DIntArray.GetLowerBound(0); i <= my1DIntArray.GetUpperBound(0); i++ )
                  my1DIntArray.SetValue( i+1, i );

         // Displays the bounds and values of the one-dimensional Array.
         strRet += "One-dimensional Array:\r\n";
         strRet += "Rank\tLower\tUpper\r\n";
         strRet += ("0" + "\t" + my1DIntArray.GetLowerBound(0).ToString() + "\t" + my1DIntArray.GetUpperBound(0).ToString() );
         strRet += "\r\nValues:\r\n";
         strRet += PrintValues(ref my1DIntArray );
         strRet += "================\r\n";

         // Creates a new three-dimensional Array of type Int32.
         Array my3DIntArray = Array.CreateInstance( typeof(Int32), 2, 3, 4 );
         // Uses GetLowerBound and GetUpperBound in the for loop.
         for ( int i = my3DIntArray.GetLowerBound(0); i <= my3DIntArray.GetUpperBound(0); i++ )
                  for ( int j = my3DIntArray.GetLowerBound(1); j <= my3DIntArray.GetUpperBound(1); j++ )
                           for ( int k = my3DIntArray.GetLowerBound(2); k <= my3DIntArray.GetUpperBound(2); k++ )
                           {
                                    my3DIntArray.SetValue( (i*100)+(j*10)+k, i, j, k );
                           }

         // Displays the bounds and values of the multidimensional Array.
         strRet += "Multidimensional Array:\r\n";
         strRet += "Rank\tLower\tUpper\r\n";
         for ( int i = 0; i < my3DIntArray.Rank; i++ )
                  strRet += (i.ToString() + "\t" + my3DIntArray.GetLowerBound(i).ToString() + "\t" + my3DIntArray.GetUpperBound(i).ToString() + "\r\n" );
         strRet += "Values:\r\n";
         strRet += PrintValues(ref my3DIntArray );

         return strRet;
}

public static string PrintValues(ref Array myArr )
{
         string strRet = "";
         System.Collections.IEnumerator myEnumerator = myArr.GetEnumerator();

         int i = 0;
         int cols = myArr.GetLength( myArr.Rank - 1 );
         while ( myEnumerator.MoveNext() )
         {
                  if ( i < cols )
                  {
                           i++;
                  }
                  else
                  {
                           strRet += "\r\n";
                           i = 1;
                  }
                  strRet += (myEnumerator.Current.ToString() + "\t");
         }
         strRet += "\r\n";
         return strRet;
}

 

Blaze++ .NET

static String DoTest()
{
         String strRet = "";

         
         // Creates a new one-dimensional Array of type Int32.
         Array my1DIntArray = Array::CreateInstance(typeof(Int32), 5 );

         // Uses GetLowerBound and GetUpperBound in the for loop.
         for ( int i = my1DIntArray.GetLowerBound(0); i <= my1DIntArray.GetUpperBound(0); i++ )
                  my1DIntArray.SetValue(i+1, i );
                   

         


         // Displays the bounds and values of the one-dimensional Array.
         strRet += "One-dimensional Array:\r\n";
         strRet += "Rank\tLower\tUpper\r\n0\t";
         strRet += Int32(my1DIntArray.GetLowerBound(0)).ToString();
         strRet += "\t";
         strRet += Int32(my1DIntArray.GetUpperBound(0)).ToString();
         strRet += "\r\nValues:\r\n ";
         strRet += PrintValues(my1DIntArray);
         strRet += "================\r\n";


                           

         // Creates a new three-dimensional Array of type Int32.
         Array my3DIntArray = Array::CreateInstance(typeof(Int32), 2, 3, 4 );

         // Uses GetLowerBound and GetUpperBound in the for loop.
         for ( int i = my3DIntArray.GetLowerBound(0); i <= my3DIntArray.GetUpperBound(0); i++ )
         {
                  for ( int j = my3DIntArray.GetLowerBound(1); j <= my3DIntArray.GetUpperBound(1); j++ )
                  {
                           for ( int k = my3DIntArray.GetLowerBound(2); k <= my3DIntArray.GetUpperBound(2); k++ )
                           {
                                    my3DIntArray.SetValue(i*100 + j*10 + k, i, j, k );
                           }
                  }
         }



         / Displays the bounds and values of the multidimensional Array.
         strRet += "Multidimensional Array:\r\n";
         strRet += "Rank\tLower\tUpper\r\n";
         for ( int i = 0; i < my3DIntArray.Rank; i++ )
         {
                  strRet += Int32(i).ToString();
                  strRet += "\t";
                  strRet += Int32(my3DIntArray.GetLowerBound(i)).ToString();
                  strRet += "\t";
                  strRet += Int32(my3DIntArray.GetUpperBound(i)).ToString();
                  strRet += "\r\n";
         }
         strRet += "Values:\r\n";
         strRet += PrintValues(my3DIntArray );

return strRet;
}

static String PrintValues(Array& myArr )
{
         String strRet = "";
         System::Collections::IEnumerator& myEnumerator = myArr.GetEnumerator();
         int i = 0;
         int cols = myArr.GetLength( myArr.Rank - 1 );


         while ( myEnumerator.MoveNext() )
         {
                  if ( i < cols )
                  {
                           i++;
                  }
                  else
                  {
                           strRet += "\r\n";
                           i = 1;
                  }
                  strRet += (myEnumerator.Current.ToString() + "\t");
         }
         strRet += "\r\n";
         return strRet;
}